package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ghost.GhostUserData;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ghost.GhostModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.ghost.GhostMgr;
import com.yanqu.road.server.pb.GhostPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_GHOST_GET_GAME_DATA, desc = "获取游戏数据")
public class GetGhostGameDataCmd extends AbstractCommand {

    private int type = 0;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.GetGhostGameDataReqMsg reqMsg = GhostActivityProto.GetGhostGameDataReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = GhostMgr.getActivityInfo();
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        GhostMgr.checkUserData(player);

        GhostUserData userData = player.getModule(GhostModule.class).getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_UNLOCK;
            return;
        }

        // 不需要跨服的数据
        if (type == 2) {
            GhostActivityProto.GetGhostGameDataRespMsg.Builder builder = GhostActivityProto.GetGhostGameDataRespMsg.newBuilder();
            builder.setRet(ret);
            builder.setUserData(GhostPb.parseGhostUserDataMsg(userData));
            player.sendPacket(ClientProtocol.U_GHOST_GET_GAME_DATA, builder);
            return;
        }

        // 需要跨服的数据
        boolean needErrorCode = true;
        if (type == 1) { // 不需要错误码
            needErrorCode = false;
        }
        GhostActivityProto.CrossGetGhostGameDataReqMsg.Builder builder = GhostActivityProto.CrossGetGhostGameDataReqMsg.newBuilder();
        builder.setActivityId(activityInfo.getActivityId());
        builder.setServerId(player.getUserInfo().getOriginalServerId());
        builder.setUserData(GhostPb.parseGhostUserDataMsg(userData));
        builder.setNeedErrorCode(needErrorCode);
        player.sendPacket(CrossProtocol.C2_GHOST_ACTIVITY_GET_GAME_DATA, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            GhostActivityProto.GetGhostGameDataRespMsg.Builder builder = GhostActivityProto.GetGhostGameDataRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_GHOST_GET_GAME_DATA, builder);
        }
    }
}
